package com.xm.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wxm.servlet.BaseServlet;
import com.xm.dao.DesignerDaolmpl;
import com.xm.entity.Designer;
import com.xm.uti.Result;

/**
 * Servlet implementation class DesingerServletAdmin
 */
@WebServlet("/admin/DesingerServletAdmin")
public class DesingerServletAdmin extends BaseServlet {

	DesignerDaolmpl disgDao = new DesignerDaolmpl();

	/**
	 * 获取设计师列表
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getDesingers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 调用dao层的方法获取数据
		ArrayList<Designer> allDesinger = disgDao.getAllDesigner();

		// 将数据封装成layui需要的格式
		String res = Result.toClient(0, "设计师列表", allDesinger, allDesinger.size());

		// 将数据返回到页面
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();

	}

	/**
	 * 添加设计师
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addDesingers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 接收传来的参数
		String name = req.getParameter("name");
		String position = req.getParameter("position");
		String url = req.getParameter("url");

		// 调用dao层的方法获取数据
		Designer desingers = new Designer(0, name, position, url);
		boolean res = disgDao.addDesinger(desingers);

		// 将数据返回到页面
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "添加成功"));
		} else {
			out.write(Result.toClient(1, "添加失败"));
		}

		out.flush();
		out.close();

	}

	/**
	 * 修改设计师
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updDesingers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 接收传来的参数
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String position = req.getParameter("position");
		String url = req.getParameter("url");
		int id1 = 0;
		if (id != null && !id.equals("")) {
			id1 = Integer.valueOf(id);
		}
		// 调用dao层的方法获取数据
		Designer desingers = new Designer(id1, name, position, url);
		System.out.println("要添加的员工对象是：" + desingers);
		boolean res = disgDao.updDesinger(desingers);

		// 将数据返回到页面
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "修改成功"));
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}

		out.flush();
		out.close();

	}

	/**
	 * 删除设计师
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delDesingers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 接收传来的参数
		String id = req.getParameter("id");
		int id1 = 0;
		if (id != null && !id.equals("")) {
			id1 = Integer.valueOf(id);
		}
		// 调用dao层的方法获取数据
		boolean res = disgDao.delDesinger(id1);

		// 将数据返回到页面
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "删除成功"));
		} else {
			out.write(Result.toClient(1, "删除失败"));
		}

		out.flush();
		out.close();

	}

}